// 这个是详情页

import 'package:enjoy_plus/utils/http.dart';
import 'package:enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
//  Map<String,dynamic> params = {};

  Map notifyDetail = {

  };

//在生命周期中 解析跳转传递过来的参数

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 获取参数并安全地处理
    final arguments = ModalRoute.of(context)?.settings.arguments;
    if (arguments != null && arguments is Map && arguments['id'] != null) {
      gettDetailData(arguments['id']);
    }
  }

  // 通过id获取详情数据
  gettDetailData(String id) async {
   final res = await http.get('/announcement/$id');
  //  
  if (res['code'] != 10000) {
    ToastUtil.showError('请求失败');
    return;
  }
  // print(res);
  ToastUtil.showSuccess('请求成功');
  setState(() {
    notifyDetail = res['data'];
  });
  }

  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('公告详情'),
      ),
      body: ListView(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题
                Text(
                  notifyDetail['title']?? '',
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  )
                ),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      notifyDetail['creatorName']?? '',
                      style: const TextStyle(
                        color: Colors.grey
                      )
                    ),
                    Text(
                      notifyDetail['createdAt']?? '',
                      style: const TextStyle(
                        color: Colors.grey
                      )
                    )
                  ]
                ),
                const SizedBox(height: 10),
                // 内容
                Html(
                  data: notifyDetail['content']?? '',
                )
              ]
            )
          )
        ]
      )
    );
  }
}